How to Remotely Shutdown or Restart Windows using ASP.NET

Create two simple batch files and put them under \bin\ directory of your webserver.

  1. The batch file for shutdownWindows.bat is

    shutdown -s -f -t 10

  2. The batch file for restartWindows.bat is

    shutdown -r -f -t 10

Comments of Parameters

  • -s - Shuts down the local computer.
  • -r - Reboots after shutdown.
  • -f - Forces running applications to close.
  • -t xx - Sets the timer for system shutdown in xx seconds. The default is 20 seconds.

And then you can just create a simple ASPX page to remotely control your computer.

Below is the remote.ASPX file

<%@ Page language="c#" Codebehind="remote.aspx.cs" AutoEventWireup="false" Inherits="restartWindows.WebForm1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

 <HEAD>

  <title>WebForm1</title>

  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

  <meta name="CODE_LANGUAGE" Content="C#">

  <meta name="vs_defaultClientScript" content="JavaScript">

  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

 </HEAD>

 <body MS_POSITIONING="GridLayout">

  <form id="Form1" method="post" runat="server">

   <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 176px" runat="server"

    Text="Shut Down"></asp:Button>

   <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 176px" runat="server"

    Text="Restart Windows"></asp:Button>

   <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 72px; POSITION: absolute; TOP: 64px" runat="server"

    Width="488px" Height="48px" Font-Bold="True" ForeColor="#000040">Sample Program to Show How to remotely Shutdown or Restart Windows</asp:Label>

  </form>

 </body>

</HTML>

Below is the remote.ASPX.CS file

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

 

namespace restartWindows

{

    /// <summary>

    /// Summary description for WebForm1.

    /// </summary>

    public class WebForm1 : System.Web.UI.Page

    {

        protected System.Web.UI.WebControls.Button Button1;

        protected System.Web.UI.WebControls.Label Label1;

        protected System.Web.UI.WebControls.Button Button2;

 

        private void Page_Load(object sender, System.EventArgs e)

        {

            // Put user code to initialize the page here

        }

 

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)

        {

            //

            // CODEGEN: This call is required by the ASP.NET Web Form Designer.

            //

            InitializeComponent();

            base.OnInit(e);

        }

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.Button1.Click += new System.EventHandler(this.Button1_Click);

            this.Button2.Click += new System.EventHandler(this.Button2_Click);

            this.Load += new System.EventHandler(this.Page_Load);

 

        }

        #endregion

 

        private void Button1_Click(object sender, System.EventArgs e)

        {

            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

            myProcess.StartInfo.WorkingDirectory = Request.MapPath("~/bin");

            myProcess.StartInfo.FileName = Request.MapPath("~/bin/shutdownWindows.bat");

            myProcess.Start();

        }

 

        private void Button2_Click(object sender, System.EventArgs e)

        {

            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

            myProcess.StartInfo.WorkingDirectory = Request.MapPath("~/bin");

            myProcess.StartInfo.FileName = Request.MapPath("~/bin/restartWindows.bat");

            myProcess.Start();

        }

    }

}

Up to now, the programs to restart and shutdown the remote computer are complete.

The next is to setup your computer that the webserver is hosted.

(I) Open C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG\machine.config. And find the tag processModel and change userName from MACHINE to SYSTEM.

<processModel enable="true" ...="" userName="SYSTEM" password="AutoGenerate" ...="" maxIoThreads="20"/>

Then save it back and restart the computer.

Thus, if you click the shutdwon button in the ASPX page, the remote computer will be shutdown. If your computer doesn't set a password, you can also click the Restart Windows button to restart the remote computer.

If your computer has the passwrod setting, you have to do the next step to let the remote computer automatically restart without human interaction.

(II) Turn on automatic logon with a password

You can use Registry Editor to add your log on information.

To do this, follow these steps: 

  1. Click Start, click Run, type regedit, and then click OK. 

  2. Locate the following registry key:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon
     
  3. Using your account name and password, double-click the DefaultUserName entry, type your user name, and then click OK.

  4. Double-click the DefaultPassword entry, type your password under the value data box, and then click OK.

    If there is no DefaultPassword value, create the value.

    To do this, follow these steps:  

    1. In Registry Editor, click Edit, click New, and then click String Value.
    2. Type DefaultPassword as the value name, and then press ENTER.
    3. Double-click the newly created key, and then type your password in the Value Data box.If no DefaultPassword string is specified, Windows XP automatically changes the value of the AutoAdminLogon registry key from 1 (true) to 0 (false) to turn off the AutoAdminLogon feature.

  5. Double-click the AutoAdminLogon entry, type 1 in the Value Data box, and then click OK.

    If there is no AutoAdminLogon entry, create the entry. To do this, follow these steps:

    1. In Registry Editor, click Edit, click New, and then click String Value.
    2. Type AutoAdminLogon as the value name, and then press ENTER.
    3. Double-click the newly created key, and then type 1 in the Value Data box.

  6. Quit Registry Editor.

  7. Click Start, click Restart, and then click OK.

After your computer restarts and Windows XP starts, you can log on automatically.

After you set up (II), you can remotely restart and shutdown your computer by the ASP.NET.


Similar Articles